package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.entity.CollectEntity;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class DaqiTask extends Task{
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_DaQI;// 对应网站ID
	private static final String ENCoding =TOP10Global.Encoding_GDK;// 对应网站ID
	
	public DaqiTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new DaqiParser("社会排行",TOP10Global.TYPE_News,"http://shehui.daqi.com/","rdjx_r_b_1","http://shehui.daqi.com"));
		PARSER.add(new DaqiParser("理财",TOP10Global.TYPE_Finance,"http://cf.daqi.com","rdjx_r_b_1","http://cf.daqi.com"));
		PARSER.add(new DaqiParser("娱乐排行",TOP10Global.TYPE_ENT,"http://ent.daqi.com/","rdjx_r_b_1","http://ent.daqi.com"));
		PARSER.add(new DaqiParser("情感排行",TOP10Global.TYPE_Emotion,"http://love.daqi.com","rdjx_r_b_1","http://love.daqi.com"));
		PARSER.add(new DaqiParser("论坛排行",TOP10Global.TYPE_Forum,"http://bbs.daqi.com","clic","http://bbs.daqi.com","class"));
		PARSER.add(new DaqiParser("排行",TOP10Global.TYPE_PIC,"http://pic.daqi.com/","rdph_b_1","http://pic.daqi.com","id","table"));
		
	}
	public static void main(String arg[]) throws Exception{
		Task t=new DaqiTask();
		t.loadParser();
		t.PARSER.get(3).dealText(TOP10Global.Encoding_GDK);
	
	}
	
}
 class DaqiParser  extends TOPParserImpl{
	 private String sign;
	 private String baseUrl;
	 private String arrName;
	 private String tagName;
	public DaqiParser(String c, int t, String u,String sign,String bu) {
		super(c, t, u);
		this.sign=sign;
		this.baseUrl=bu;
	}
	public DaqiParser(String c, int t, String u,String sign,String bu,String arrName) {
		super(c, t, u);
		this.sign=sign;
		this.baseUrl=bu;
		this.arrName=arrName;
	}
	public DaqiParser(String c, int t, String u,String sign,String bu,String arrName,String tagName) {
		this(c,t,u,sign,bu,arrName);
		this.tagName=tagName;
	}
	public  List<CollectEntity> parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		Node node=null;
		
		if(arrName!=null&&tagName!=null){
			node=getNodeByAttr(tagName, arrName,sign,body);
		}else if(arrName==null&&tagName!=null){
			node=getNodeByAttr(tagName,"id",sign,body);
		}else if(tagName==null&&arrName!=null){
			node=getNodeByAttr("ul",arrName,sign,body);
		}else{
			node=getNodeByAttr("ul","id",sign,body);
		}
		//node=getChildByName(node,"ul");
		ArrayList<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		int rank=1;
		for (int i = 0; i < as.size(); i++) {
			Node tr=as.get(i);
			CollectEntity v =build(tr,rank++);
			System.out.println(v);
			list.add(v);
			if(i>=9)break;
		}
		return list;
	}

	public  void build1(Node node, int rank,CollectEntity v) throws Exception {
		
	}
	public CollectEntity build(Node node, int rank) throws Exception {
		CollectEntity v=TOP10Global.getCollectEntityByType(this.getType());
		v.setRank(rank);
		String name=getAttrValueByTag(node,"title");
		if(name==null||name.trim().equals("")){
			v.setName(node.getTextContent());
		}else{
			v.setName(name);
		}
		String url=getAttrValueByTag(node,"href");
		v.setLinkUrl(baseUrl+url);
		return v;
	}
	
}